/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.scene.service.impl;


import com.koron.common.bean.StaffBean;
import com.koron.scene.bean.StdCertificate;
import com.koron.scene.bean.StdJobs;
import com.koron.scene.domain.StdJobsCertificate;
import com.koron.scene.mapper.StdJobsCertificateMapper;
import com.koron.scene.service.api.StdJobsCertificateService;
import com.koron.scene.utils.BaseEntityUtil;
import com.koron.scene.utils.BusinessCode;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

@Service
public class StdJobsCertificateServiceImpl implements StdJobsCertificateService {

    private static final Logger logger = LoggerFactory.getLogger(ProjectServiceImpl.class);

    @Override
    public MessageBean<?> list() {
        MessageBean<List> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), List.class);
        try (SessionFactory factory = new SessionFactory()) {
            StdJobsCertificateMapper mapper = factory.getMapper(StdJobsCertificateMapper.class);
            List<StdJobs> jobs = mapper.list(null);
            msg.setData(jobs);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription(BusinessCode.FAILURE.getDescription());
        }
        return msg;
    }

    @Override
    public MessageBean<StdJobs> queryById(String stdJobId) {
        MessageBean<StdJobs> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), StdJobs.class);
        if (StringUtils.isBlank(stdJobId)) {
            msg.setCode(29001);
            msg.setDescription("标准岗位工种id不能为空");
            return msg;
        }
        try (SessionFactory factory = new SessionFactory()) {
            StdJobsCertificateMapper mapper = factory.getMapper(StdJobsCertificateMapper.class);
            List<StdJobs> jobsList = mapper.list(stdJobId);
            if (jobsList != null && !jobsList.isEmpty()) {
                StdJobs jobs = jobsList.get(0);
                msg.setData(jobs);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription(BusinessCode.FAILURE.getDescription());
        }
        return msg;
    }

    @Override
    public MessageBean<?> save(StaffBean user, StdJobs stdJobs) {
        MessageBean<StdJobs> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), StdJobs.class);
        if (stdJobs == null) {
            msg.setCode(29001);
            msg.setDescription("保存内容不能为空");
            return msg;
        }
        SessionFactory factory = new SessionFactory();
        try {
            StdJobsCertificateMapper mapper = factory.getMapper(StdJobsCertificateMapper.class);
            if (StringUtils.isBlank(stdJobs.getStdJobsId())) {
                msg.setCode(29001);
                msg.setDescription("保存岗位工种的id不能为空");
                return msg;
            }
            mapper.deleteByJobsId(stdJobs.getStdJobsId());
            List<StdJobsCertificate> list = new ArrayList<>();
            if (stdJobs.getStdCertificateList() != null && !stdJobs.getStdCertificateList().isEmpty()) {
                for (StdCertificate stdCertificate : stdJobs.getStdCertificateList()) {
                    StdJobsCertificate stdJobsCertificate = new StdJobsCertificate();
                    stdJobsCertificate.setStdJobsId(stdJobs.getStdJobsId());
                    stdJobsCertificate.setStdCertificateId(stdCertificate.getStdCertificateId());
                    BaseEntityUtil.setAddBaseProperties(stdJobsCertificate, user);
                    list.add(stdJobsCertificate);
                }
                if (list != null && !list.isEmpty()) {
                    mapper.insertList(list);
                }
            }
        } catch (Exception e) {
            factory.close(false);
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription(BusinessCode.FAILURE.getDescription());
        } finally {
            if (factory != null) {
                factory.close();
            }
        }
        return msg;
    }
}
